<?php
session_start();

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];


$url = "http://localhost:8080/moms-diary/services/authenticate";

$context = stream_context_create(array(
		'http' => array(
				'header'  => "Authorization: Basic " . base64_encode($username.":".$password)
		)
));

$data = file_get_contents($url, false, $context);

if(isset($data)) {
	$json = json_decode($data, TRUE);
	print_r($json);
	
	if($json['emailAddress']== $username ){
		
		$_SESSION['login'] = md5($username.$password);
		$_SESSION['userid'] = $json['userId'];
		$_SESSION['username'] = $username;
		$_SESSION['password'] = $password;

		
		if($json['activate']== "true"){
			header("Location: home.php");
		}else{
			header("Location: activation.php");
		}
	}else{
		header("Location: index.php");
		//echo "tess";
	}
}else{
	echo "failed";
}

